package com.cloud.business.service.sys.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.business.dao.sys.SysRegMapStructMapper;
import com.cloud.business.entity.sys.SysRegMapStruct;
import com.cloud.business.entity.sys.SysUser;
import com.cloud.business.model.commonvm.DataDropDown;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.LngAndLat;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysregmapstruct.MapStatisModel;
import com.cloud.business.model.sys.sysregmapstruct.MapStructDropDown;
import com.cloud.business.model.sys.sysregmapstruct.SysRegMapStructPost;
import com.cloud.business.model.sys.sysregmapstruct.SysRegMapStructQueryModel;
import com.cloud.business.service.sys.ISysRegMapStructService;
import com.cloud.core.common.HttpContentResult;
import com.cloud.core.utils.Path2DUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 地区架构表 业务实现层
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author Rex
 * @since 2020-09-14 17:27:49
 */
@Service
public class SysRegMapStructServiceImpl extends ServiceImpl
        <SysRegMapStructMapper, SysRegMapStruct> implements ISysRegMapStructService {

    @Resource
    private SysRegMapStructMapper sysRegMapStructMapper;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysRegMapStructQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysRegMapStructQueryModel sysRegMapStructQueryModel, IPage<SysRegMapStruct> page, ResultMsg msg) throws Exception {
        QueryWrapper<SysRegMapStruct> sysRegMapStructQueryWrapper = new QueryWrapper<>();
        sysRegMapStructQueryWrapper.lambda()
                .orderByAsc(SysRegMapStruct::getSort);

        if (page.getSize() == 0) {
            List<SysRegMapStruct> sysRegMapStructList = sysRegMapStructMapper.selectList(sysRegMapStructQueryWrapper);
            page.setRecords(sysRegMapStructList);
            page.setTotal(sysRegMapStructList.size());
        } else {
            sysRegMapStructMapper.selectPage(page, sysRegMapStructQueryWrapper);
        }
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param post
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysRegMapStructPost post, ResultMsg msg) throws Exception {
        Date now = new Date();
        SysRegMapStruct sysRegMapStruct = new SysRegMapStruct();
        BeanUtils.copyProperties(post, sysRegMapStruct);
        sysRegMapStruct.setCreateTime(now);
        sysRegMapStruct.setModificationTime(now);
        int count = sysRegMapStructMapper.insert(sysRegMapStruct);
        msg.setMsg("创建成功");
        return sysRegMapStruct.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param post
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysRegMapStructPost post, ResultMsg msg) throws Exception {

        SysRegMapStruct sysRegMapStruct = sysRegMapStructMapper.selectById(post.getObjectId());
        BeanUtils.copyProperties(post, sysRegMapStruct);
        Date now = new Date();
        sysRegMapStruct.setModificationTime(now);
        int count = sysRegMapStructMapper.updateById(sysRegMapStruct);
        msg.setMsg("更新成功");
        return sysRegMapStruct.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysRegMapStruct getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysRegMapStruct sysRegMapStruct = sysRegMapStructMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysRegMapStruct;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysRegMapStructMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

    //region 启用/停用

    /**
     * 启用/停用
     *
     * @param operationId
     * @param objectId
     * @param isEnable
     * @param msg
     * @throws Exception
     */
    @Override
    public void enable(Integer operationId, Integer objectId, Integer isEnable, ResultMsg msg) throws Exception {
        msg.setMsg("更新成功");
        if (isEnable == null) {
            throw new Exception("启用/停用标志不能为空");
        }
        SysRegMapStruct sysRegMapStruct = sysRegMapStructMapper.selectById(objectId);
        if (sysRegMapStruct == null) {
            throw new Exception("区域不存在");
        }
        sysRegMapStruct.setIsEnable(isEnable);
        int count = sysRegMapStructMapper.updateById(sysRegMapStruct);
        if (count <= 0) {
            throw new Exception("更新失败");
        }
    }
    //endregion

    //region 判断当前经纬度在哪个区域

    /**
     * 判断当前经纬度在哪个区域
     *
     * @param lngAndLat
     * @throws Exception
     */
    @Override
    public HttpContentResult checkReg(LngAndLat lngAndLat) throws Exception {
        if (lngAndLat == null) {
            throw new Exception("经纬度不能为空");
        }

        QueryWrapper<SysRegMapStruct> sysRegMapStructQueryWrapper = new QueryWrapper<>();
        sysRegMapStructQueryWrapper.lambda()
                .orderByAsc(SysRegMapStruct::getSort);
        List<SysRegMapStruct> sysRegMapStructList = sysRegMapStructMapper.selectList(sysRegMapStructQueryWrapper);

        for (SysRegMapStruct sysRegMapStruct : sysRegMapStructList
        ) {
            JSONArray jsonArray = sysRegMapStruct.getRegCollection();
            List<LngAndLat> lngAndLatList = JSON.parseArray(jsonArray.toJSONString(), LngAndLat.class);
            if (Path2DUtils.isPoint(lngAndLatList, lngAndLat.getLng(), lngAndLat.getLat())) {
                Map<String, Object> map = new HashMap<>();
                map.put("regId", sysRegMapStruct.getObjectId());
                map.put("regName", sysRegMapStruct.getRegName());
                return HttpContentResult.successResult("获取成功", 1L, map);
            }
        }

        throw new Exception("不在系统区域内");

    }
    //endregion

    //region 获取下拉列表

    /**
     * 获取下拉列表
     *
     * @param operationId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public List<MapStructDropDown> getDropDownList(Integer operationId, ResultMsg msg) throws Exception {
        List<MapStructDropDown> dataDropDownList = sysRegMapStructMapper.getDropDownList();
        msg.setMsg("获取成功");
        return dataDropDownList;
    }
    //endregion

    //region 按登录人获取区域下拉

    /**
     * 获取下拉列表
     *
     * @param operationId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public List<MapStructDropDown> getDropDownListByUserId(Integer operationId, ResultMsg msg) throws Exception {

        String cityCode = "";
        //判断当前登陆人是哪个区  获取本区的数据 如果是省则获取所有的数据
        if (operationId != null && operationId != 0) {
            MapStatisModel mapStatisModel = sysRegMapStructMapper.getAdcodeByUserId(operationId);
            if (mapStatisModel != null && !"province".equals(mapStatisModel.getLevel())) {
                cityCode=mapStatisModel.getCityCode();
            }
        }

        List<MapStructDropDown> dataDropDownList = sysRegMapStructMapper.getDropDownListByUserId(cityCode);

        msg.setMsg("获取成功");
        return dataDropDownList;
    }
    //endregion

}